package com.book.core.util;

import com.book.core.mode.SysLoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class LoginUserUtil {
    private LoginUserUtil() {
    }

    public static Integer getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysLoginUser loginUser = (SysLoginUser) authentication.getPrincipal();
        return loginUser.getSysUser().getUserId();
    }
    public static SysLoginUser getLoginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (SysLoginUser) authentication.getPrincipal();
    }
    public static void debugAuth() {
        // 获取当前认证信息
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        // 打印用户信息和权限
        System.out.println("当前用户: " + auth.getName());
        System.out.println("权限列表:");
        auth.getAuthorities().forEach(authority -> {
            System.out.println("- " + authority.getAuthority());
        });
    }
}
